package net.yunxinyong.cec.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.yunxinyong.cec.entity.Cec711BaseSituation;
import net.yunxinyong.cec.entity.Cec711BaseSituationExample;
import net.yunxinyong.cec.mapper.Cec711BaseSituationMapper;
import net.yunxinyong.cec.service.Cec711BaseService;
import net.yunxinyong.cec.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
/**
 * @author 李跃
 * @date 2018/9/17
 */
@Service
public class Cec711BaseServiceImpl implements Cec711BaseService {
    @Autowired
    private Cec711BaseSituationMapper cec711BaseSituationMapper;
    @Override
    public PageBean ListAll(int page, int rows,Cec711BaseSituation cec711BaseSituation) {
        Cec711BaseSituationExample cec711BaseSituationExample = new Cec711BaseSituationExample();
        //开启分页
        if(page !=0 && rows!=0 ){
            PageHelper.startPage(page,rows);
        }
        List<Cec711BaseSituation> cec711BaseSituationList = cec711BaseSituationMapper.selectByExample(cec711BaseSituationExample);
        //设置分页信息
        PageBean<Cec711BaseSituation> pageBean = new PageBean<Cec711BaseSituation>();
        PageInfo<Cec711BaseSituation> pageInfo = new PageInfo<Cec711BaseSituation>(cec711BaseSituationList);
        pageBean.setTotal(pageInfo.getTotal());
        pageBean.setTotalPages(pageInfo.getPages());
        pageBean.setPageDatas(cec711BaseSituationList);

        return pageBean;
    }

    /**
     * 添加单位基础信息
     * @param cec711BaseSituation
     */
    @Override
    public Cec711BaseSituation insertBase(Cec711BaseSituation cec711BaseSituation) {
        Cec711BaseSituationExample cec711BaseSituationExample = new Cec711BaseSituationExample();
        cec711BaseSituationExample.createCriteria().andUnitTypeEqualTo(cec711BaseSituation.getUnitType());
        List<Cec711BaseSituation> cec711BaseSituations = cec711BaseSituationMapper.selectByExample(cec711BaseSituationExample);
        if(!cec711BaseSituations.isEmpty()){
            return null;
        }
        //添加时间
        cec711BaseSituation.setCreateTime(new Date());
        cec711BaseSituation.setUpdateTime(new Date());
        int i = cec711BaseSituationMapper.insertSelective(cec711BaseSituation);
        return cec711BaseSituation;
    }

    @Override
    public int updateBase(Cec711BaseSituation cec711BaseSituation) {
        int i = cec711BaseSituationMapper.updateByPrimaryKeySelective(cec711BaseSituation);
        return i;
    }

    @Override
    public int deleteBase(Integer id) {
        Cec711BaseSituation cec711BaseSituation = new Cec711BaseSituation();
        cec711BaseSituation.setId(id);
        cec711BaseSituation.setStatus(0);
        int i = cec711BaseSituationMapper.updateByPrimaryKeySelective(cec711BaseSituation);
        return i;
    }
}
